﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cofe.Core.Script
{
    public class ScriptCommandManager  : IScriptCommandProviderManager
    {

        #region Constructor
        
        #endregion

        #region Methods

        public IScriptCommand GetCommand(string command)
        {
            foreach (var c in Commands)
                if (c.CommandKey.Equals(command, StringComparison.CurrentCultureIgnoreCase))
                    return c;
            return null;
        }

        IEnumerable<IScriptCommand> getAllCommands()
        {
            List<IScriptCommand> retVal = new List<IScriptCommand>();

            foreach (var provider in CofeServices.ServiceLocater.FindAllServices<IScriptCommandProvider>())
                retVal.AddRange(provider.GetCommands());

            return retVal;
        }

        #endregion

        #region Data

        IEnumerable<IScriptCommand> _commands = null;

        #endregion

        #region Public Properties

        public IEnumerable<IScriptCommand> Commands
        {
            get { return _commands ?? (_commands = getAllCommands()); }
        }

        #endregion

        

        
    }
}
